package com.zz.yt.lib.bluetooth.delegate.print.mht;


import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;

import androidx.annotation.NonNull;

import com.blankj.utilcode.util.ToastUtils;
import com.mht.print.sdk.PrinterConstants;
import com.mht.print.sdk.PrinterInstance;
import com.whf.android.jar.util.log.LatteLogger;
import com.whf.android.jar.util.storage.LattePreference;
import com.zz.yt.lib.bluetooth.base.BaseBluetoothDelegate;
import com.zz.yt.lib.bluetooth.base.Bluetooth;



/**
 * 蓝牙连接
 *
 * @author qf
 * @version 1.0
 **/
public abstract class BaseBluetoothMhtDelegate extends BaseBluetoothDelegate {

    protected PrinterInstance mPrinter;

    @SuppressLint("HandlerLeak")
    public Handler mHandler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            if (msg.what == PrinterConstants.Connect.SUCCESS) {
                LatteLogger.i("SUCCESS");
            } else {
                connection();
            }
        }
    };

    @Override
    protected void inRx() {
        //连接成功
        mPrinter = new PrinterInstance(context, mDevice, mHandler);
        final String device = LattePreference.getCustomAppProfile(Bluetooth.BLUETOOTH);
        ToastUtils.showShort(device + "连接成功");
        connection();
    }

    protected void connection() {
        try {
            if (mPrinter != null) {
                mPrinter.openConnection();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void closeConnection() {
        try {
            if (mPrinter != null) {
                mPrinter.closeConnection();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
